package com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CAshGateVideoBind;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CCompanys;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TCoalYardArea;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.TCoalYardAreaMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITCoalYardAreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.AshGateBindCoalYardVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.TCoalYardAreaVo;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lvNuo
 * @since 2025-04-21
 */
@Service
public class TCoalYardAreaServiceImpl extends ServiceImpl<TCoalYardAreaMapper, TCoalYardArea> implements ITCoalYardAreaService {

    @Autowired
    private CAshGateVideoBindServiceImpl ashGateVideoBindService;

    @Override
    public  Map<String, Object> getList(Integer page, Integer pageSize, String ashGateId) {
        QueryWrapper<TCoalYardArea> coalYardAreaQueryWrapper = new QueryWrapper<>();
        coalYardAreaQueryWrapper.eq(StringUtils.isNotEmpty(ashGateId),"ASH_GATE_ID",ashGateId);
        coalYardAreaQueryWrapper.orderByDesc("ID");
        Page<TCoalYardArea> coalYardAreaPage = this.page(new Page<>(page,pageSize),coalYardAreaQueryWrapper);
        List<TCoalYardAreaVo> list = coalYardAreaPage.getRecords().stream().map(item -> {
            CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getById(item.getAshGateId());
            TCoalYardAreaVo coalYardAreaVo = new TCoalYardAreaVo();
            BeanUtils.copyProperties(item, coalYardAreaVo);
            if (ashGateVideoBind != null) {
                coalYardAreaVo.setAshgateName(ashGateVideoBind.getAshgateName());
            }
            return coalYardAreaVo;
        }).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("total", coalYardAreaPage.getTotal());
        map.put("list", list);
        return map;
    }

    @Override
    public  List<TCoalYardAreaVo> getAllList() {
        QueryWrapper<TCoalYardArea> coalYardAreaQueryWrapper = new QueryWrapper<>();
        List<TCoalYardAreaVo> list = this.list(coalYardAreaQueryWrapper).stream().map(item -> {
            CAshGateVideoBind ashGateVideoBind = ashGateVideoBindService.getById(item.getAshGateId());
            TCoalYardAreaVo coalYardAreaVo = new TCoalYardAreaVo();
            BeanUtils.copyProperties(item, coalYardAreaVo);
            if (ashGateVideoBind != null) {
                coalYardAreaVo.setAshgateName(ashGateVideoBind.getAshgateName());
            }
            return coalYardAreaVo;
        }).collect(Collectors.toList());
        return list;
    }
    @Override
    public List<AshGateBindCoalYardVo> getAshGateBindCoalList() {
        // 查询 CAshGateVideoBind 数据
        QueryWrapper<CAshGateVideoBind> ashGateVideoBindQueryWrapper = new QueryWrapper<>();
        ashGateVideoBindQueryWrapper.eq("AREA_ID", "1397a7bae240383152dfa1161da93d2d");
        List<CAshGateVideoBind> ashGateVideoBindList = ashGateVideoBindService.list(ashGateVideoBindQueryWrapper);

        // 转换为 AshGateBindCoalYardVo 并填充 coalYardAreaList
        return ashGateVideoBindList.stream().map(item -> {
            // 创建 AshGateBindCoalYardVo 对象
            AshGateBindCoalYardVo ashGateBindCoalYardVo = new AshGateBindCoalYardVo();

            // 复制 CAshGateVideoBind 的属性到 AshGateBindCoalYardVo
            BeanUtils.copyProperties(item, ashGateBindCoalYardVo);

            // 查询对应的 TCoalYardArea 数据
            QueryWrapper<TCoalYardArea> coalYardAreaQueryWrapper = new QueryWrapper<>();
            coalYardAreaQueryWrapper.eq("ASH_GATE_ID", item.getId());
            List<TCoalYardArea> coalYardAreaList = this.list(coalYardAreaQueryWrapper);

            // 设置 coalYardAreaList 到 AshGateBindCoalYardVo
            ashGateBindCoalYardVo.setCoalYardAreaList(coalYardAreaList);

            return ashGateBindCoalYardVo;
        }).collect(Collectors.toList());
    }

    @Override
    public SzResult addCoalYardArea(TCoalYardArea coalYardArea) {
        QueryWrapper<TCoalYardArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("CAST(ID AS SIGNED)"); // 将 varchar 转换为数值类型
        queryWrapper.last("LIMIT 1");
        TCoalYardArea coalYardArea1 = this.getOne(queryWrapper);
        QueryWrapper<TCoalYardArea> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ASH_GATE_ID", coalYardArea.getAshGateId());
        queryWrapper1.eq("ASH_GATE_AREA", coalYardArea.getAshGateArea());
        if (getOne(queryWrapper1) != null) {
            return SzResult.error("该AreaId已存在");
        }
        if (coalYardArea1 != null) {
            BigDecimal coalYardAreaId = new BigDecimal(coalYardArea1.getId());
            String coalYardAreaId1 = String.valueOf((coalYardAreaId.intValue() + 1));
            coalYardArea.setId(coalYardAreaId1);
        } else {
            coalYardArea.setId("1");
// cAshGateVideoBind.setAshgateId(uuid);
        }
        if (save(coalYardArea)) {
            return  SzResult.success("新增成功");
        }
        return SzResult.error("新增失败");
    }

    @Override
    public SzResult updateCoalYardArea(TCoalYardArea coalYardArea) {
        if (updateById(coalYardArea)) {
            return SzResult.success("修改成功");
        }
        return SzResult.error("修改失败");
    }

    @Override
    public SzResult delCoalYardArea(String id) {
        if (removeById(id)) {
            return SzResult.success("删除成功");
        }
        return SzResult.error("删除失败");
    }
}
